package com.lottery.pay.service.impl;

import com.lottery.pay.Constants.WeixinConstant;
import com.lottery.pay.Model.weixin.RefundParam;
import com.lottery.pay.Utils.wxUtil.HttpRequest;
import com.lottery.pay.Utils.wxUtil.MainUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.TreeMap;

@Service
public class RefundService {
	public static final Logger logger = LoggerFactory.getLogger(RefundService.class);

	/**
	 * 退款接口
	 */
	public String payoff(int payType, String out_trade_no,
                         String out_refund_no, String total_fee, String refund_fee) {
		// 请求prepay_id
		// String out_trade_no = "201511021451190436";
		// String out_refund_no = "201511021451190436";
		// String total_fee = "100";// 总金额 单位为分
		// String refund_fee = "1";// 退款金额 单位为分
		// 支付类型 0:微信公众号支付 1:APP支付
		RefundParam refundParam = null;
		refundParam = new RefundParam(WeixinConstant.APP_APPID,WeixinConstant.MCH_ID, out_trade_no,
					out_refund_no, total_fee, refund_fee);

		// object转化为TreeMap
		TreeMap<Object, Object> treeMap = MainUtil.objectToTreeMap(refundParam);
		// 生成签名
		String sign = MainUtil.creatSign(treeMap, WeixinConstant.API_KEY);
		logger.info("pay off sign is {}",sign);
		refundParam.setSign(sign);
		String xmlString = refundParam.toXmlString(refundParam);
		logger.info("weixin payoff orderXml={}", xmlString);
		String response = null;
		response = HttpRequest
					.httpClient(xmlString, WeixinConstant.REFUND_PATH,
							WeixinConstant.SSL_CRET, WeixinConstant.MCH_ID);

		logger.info("weixin payoff response={}", response);

		return response;
	}

}
